package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class MyDAO {

    private static Connection con;

	public static Connection getConnection() throws Exception {

		if (con == null || con.isClosed()) {
			String driverName = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://localhost:3306/autoirrgation?characterEncoding=utf-8";
			String userName = "root";
			String password = "";
			Class.forName(driverName);
			con = DriverManager.getConnection(url, userName, password);
		}
		return con;
	}

	/**
	 * 普通statement
	 * @return
	 * @throws Exception
	 */
	public static Statement getStatement() throws Exception {
		Statement stmt = getConnection().createStatement();
		return stmt;
	}
	
	/**
	 * 预处理的statement
	 * @param sql
	 * @return
	 * @throws SQLException
	 * @throws Exception
	 */
	public static PreparedStatement getPreparedStatement(String sql) throws SQLException, Exception {
		PreparedStatement preparedStatement = getConnection().prepareStatement(sql);
		return preparedStatement;
	}
}